//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package dao

import (
	"context"

	"gitcode.com/eteam/api-server/dao/board_dao"
	"gitcode.com/eteam/api-server/dao/config_dao"
	"gitcode.com/eteam/api-server/dao/fs_dao"
	"gitcode.com/eteam/api-server/dao/user_dao"
	"github.com/redis/go-redis/v9"
	"go.mongodb.org/mongo-driver/mongo"
)

var _mongoClient *mongo.Client

var CacheDao *_CacheDao
var VersionDao *_VersionDao

func InitDao(mgoDb *mongo.Database, rdb *redis.Client) (err error) {
	_mongoClient = mgoDb.Client()
	CacheDao, err = newCacheDao(rdb)
	if err != nil {
		return
	}
	VersionDao, err = newVersionDao(mgoDb)
	if err != nil {
		return
	}
	err = user_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = fs_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = board_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = config_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	return nil
}

func StartSession() (mongo.Session, error) {
	return _mongoClient.StartSession()
}

func EndSession(sess mongo.Session) {
	sess.EndSession(context.Background())
}
